package objects;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.TreeMap;

public class Item {

    private int guid, type, level, weight, price, slot = -1;
    private String name, weaponInfo, useEffects;
    private boolean isTwoHands, isEtheral, isForgemagable, isBuff, isUsable, isTargetable;
    private Hashtable<Integer, Condition> conditions = new Hashtable<Integer, Condition>();
    private Hashtable<Integer, String> effects = new Hashtable<Integer, String>();

    public Item() {
    }

    public Item(int pGuid, String pName, int pType, int pLevel, int pWeight, String pWeaponInfo,
            boolean pTwoHands, boolean pIsEtheral, boolean pIsForgemagable, boolean pIsBuff,
            boolean pUsable, boolean pTargetable, int pPrice, String pConditions,
            String pStats, String pUseEffects) {
        guid = pGuid;
        name = pName;
        type = pType;
        level = pLevel;
        weight = pWeight;
        weaponInfo = pWeaponInfo;
        isTwoHands = pTwoHands;
        isEtheral = pIsEtheral;
        isForgemagable = pIsForgemagable;
        isBuff = pIsBuff;
        isUsable = pUsable;
        isTargetable = pTargetable;
        price = pPrice;
        conditions.put(0, new Condition(pConditions)); // FIXME : Create conditions parser to store conditions
        effects.put(0, pStats); // FIXME : Create stats parser to store stats
        useEffects = pUseEffects;
    }

    public static String formatEquipedItem(TreeMap<Integer, Item> equipedItem) {
        Iterator<Item> vIter = equipedItem.values().iterator();
        String itemsFormatted = "";

        while (vIter.hasNext()) {
            Item value = vIter.next();

            itemsFormatted += Integer.toHexString(value.getGuid());
            itemsFormatted += (vIter.hasNext() ? "," : "");
        }

        return itemsFormatted;
    }

    public static HashMap<String, String[]> parseItem(String stats) // FIXME : Souled Fuck You Mother Fucker
    {
        // Get all effects
        String[] effects;
        effects = stats.split(",");

        HashMap<String, String[]> effectList = new HashMap<String, String[]>();
        String[] effectDatas = {"", "", "", "", ""};

        // Get stats by effects
        for (String eff : effects) {
            // Effect type
            String[] splittedEffects = eff.split("#");

            if (splittedEffects[2].equals("0")) // Unique jet
            {
                effectDatas[0] = splittedEffects[0]; // Effect type
                effectDatas[1] = splittedEffects[1]; // Max jet
                effectDatas[2] = splittedEffects[2]; // Min jet
                effectDatas[3] = splittedEffects[3]; // ?
                effectDatas[4] = splittedEffects[4]; // Jet
            } else // Jet min & max
            {
                effectDatas[0] = splittedEffects[0]; // Effect type
                effectDatas[1] = splittedEffects[1]; // Effect value
                effectDatas[2] = splittedEffects[2]; // 0 because it's a bonus
                effectDatas[3] = splittedEffects[3]; // ?
                effectDatas[4] = splittedEffects[4]; // Jet
            }

            effectList.put(splittedEffects[0], effectDatas.clone());
        }

        return effectList;
    }

    public int getGuid() {
        return guid;
    }

    public void setGuid(int guid) {
        this.guid = guid;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getWeaponInfo() {
        return weaponInfo;
    }

    public void setWeaponInfo(String weaponInfo) {
        this.weaponInfo = weaponInfo;
    }

    public String getUseEffects() {
        return useEffects;
    }

    public void setUseEffects(String useEffects) {
        this.useEffects = useEffects;
    }

    public boolean isTwoHands() {
        return isTwoHands;
    }

    public void setTwoHands(boolean isTwoHands) {
        this.isTwoHands = isTwoHands;
    }

    public boolean isEtheral() {
        return isEtheral;
    }

    public void setEtheral(boolean isEtheral) {
        this.isEtheral = isEtheral;
    }

    public boolean isForgemagable() {
        return isForgemagable;
    }

    public void setForgemagable(boolean isForgemagable) {
        this.isForgemagable = isForgemagable;
    }

    public boolean isBuff() {
        return isBuff;
    }

    public void setBuff(boolean isBuff) {
        this.isBuff = isBuff;
    }

    public boolean isUsable() {
        return isUsable;
    }

    public void setUsable(boolean isUsable) {
        this.isUsable = isUsable;
    }

    public boolean isTargetable() {
        return isTargetable;
    }

    public void setTargetable(boolean isTargetable) {
        this.isTargetable = isTargetable;
    }

    public Hashtable<Integer, Condition> getConditions() {
        return conditions;
    }

    public void setConditions(Hashtable<Integer, Condition> conditions) {
        this.conditions = conditions;
    }

    public Hashtable<Integer, String> getEffects() {
        return effects;
    }

    public void addEffects(String effect) {
        this.effects.put(0, effect);
    }

    public int getSlot() {
        return slot;
    }

    public void setSlot(int slot) {
        this.slot = slot;
    }

    public static String formatedIventory(Hashtable<Integer, Object[]> inventory) {
        Iterator<Object[]> vIter = inventory.values().iterator();
        String inventoryString = "";

        while (vIter.hasNext()) {
            Object[] value = vIter.next();

            inventoryString += Integer.toHexString(((Item) value[0]).getGuid()) + "~";
            inventoryString += Integer.toHexString(((Item) value[0]).getGuid()) + "~";
            inventoryString += Integer.toHexString((Integer) value[1]) + "~";
            inventoryString += ((Item) value[0]).getSlot() == -1 ? "" : Integer.toHexString(((Item) value[0]).getSlot()) + "~";
            inventoryString += formatedEffectsASK(((Item) value[0]).getEffects());
            inventoryString += (vIter.hasNext() ? ";" : "");

            vIter.remove();
        }

        return inventoryString;
    }

    /*public static String formatedIventory(TreeMap<Integer, Item> inventory)
    {
    Iterator<Item> vIter = inventory.values().iterator();
    String inventoryString = "";
    int i = 2;
    
    while(vIter.hasNext())
    {
    i++;
    Item value = vIter.next();
    
    inventoryString += Integer.toHexString(value.getGuid()) + "~";
    inventoryString += Integer.toHexString(value.getGuid()) + "~";
    inventoryString += Integer.toHexString(1) + "~";
    inventoryString += Integer.toHexString(i) + "~";
    inventoryString += formatedEffectsASK(value.getEffects());
    inventoryString += (vIter.hasNext() ? ";" : "");
    }
    
    return inventoryString;
    }*/
    public static String formatedEffectsASK(Hashtable<Integer, String> effects) {
        Iterator<String> vIter = effects.values().iterator();
        String formatedEffects = "";

        while (vIter.hasNext()) {
            String value = vIter.next();

            formatedEffects += value;
            formatedEffects += (vIter.hasNext()) ? "," : "";
        }

        return formatedEffects;
    }
}
